package models

import (
	"github.com/beego/beego/v2/client/orm"
)

// PaymentSetting 支付设置模型
type PaymentSetting struct {
	BaseModel
	Key         string `orm:"size(50);unique" json:"key"`   // 设置键名
	Value       string `orm:"type(text)" json:"value"`      // 设置值
	Description string `orm:"size(255)" json:"description"` // 描述
	Status      int8   `orm:"default(1)" json:"status"`     // 1:启用, 0:禁用
	Group       string `orm:"size(50);index" json:"group"`  // 分组
	SortOrder   int    `orm:"default(0)" json:"sort_order"` // 排序
}

// TableName 设置表名
func (p *PaymentSetting) TableName() string {
	return "ss_payment_setting"
}

// GetPaymentSettingById 根据ID获取支付设置
func GetPaymentSettingById(id int64) (*PaymentSetting, error) {
	setting := &PaymentSetting{BaseModel: BaseModel{Id: id}}
	o := orm.NewOrm()
	err := o.Read(setting)
	return setting, err
}

// GetPaymentSettingByKey 根据键名获取支付设置
func GetPaymentSettingByKey(key string) (*PaymentSetting, error) {
	setting := &PaymentSetting{Key: key}
	o := orm.NewOrm()
	err := o.Read(setting, "Key")
	return setting, err
}

// GetPaymentSettingList 获取支付设置列表
func GetPaymentSettingList(page, pageSize int, filters map[string]interface{}) ([]*PaymentSetting, int64, error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(PaymentSetting))

	// 应用过滤条件
	if key, ok := filters["key"].(string); ok && key != "" {
		qs = qs.Filter("key__contains", key)
	}

	if group, ok := filters["group"].(string); ok && group != "" {
		qs = qs.Filter("group", group)
	}

	if status, ok := filters["status"].(int8); ok {
		qs = qs.Filter("status", status)
	}

	// 排除已删除的记录
	qs = qs.Filter("is_deleted", 0)

	// 获取总数
	total, _ := qs.Count()

	// 分页查询
	var settings []*PaymentSetting
	_, err := qs.OrderBy("group", "sort_order", "-id").Limit(pageSize, (page-1)*pageSize).All(&settings)

	return settings, total, err
}

// CreatePaymentSetting 创建支付设置
func CreatePaymentSetting(setting *PaymentSetting) (int64, error) {
	o := orm.NewOrm()
	id, err := o.Insert(setting)
	return id, err
}

// UpdatePaymentSetting 更新支付设置
func UpdatePaymentSetting(setting *PaymentSetting, fields ...string) error {
	o := orm.NewOrm()
	_, err := o.Update(setting, fields...)
	return err
}

// DeletePaymentSetting 删除支付设置（软删除）
func DeletePaymentSetting(id int64) error {
	o := orm.NewOrm()
	setting := &PaymentSetting{BaseModel: BaseModel{Id: id}}
	if err := o.Read(setting); err != nil {
		return err
	}

	setting.IsDeleted = 1
	_, err := o.Update(setting, "IsDeleted")
	return err
}

// GetPaymentSettingsByGroup 获取指定分组的支付设置
func GetPaymentSettingsByGroup(group string) ([]*PaymentSetting, error) {
	o := orm.NewOrm()
	var settings []*PaymentSetting
	_, err := o.QueryTable(new(PaymentSetting)).Filter("group", group).Filter("status", 1).Filter("is_deleted", 0).OrderBy("sort_order").All(&settings)
	return settings, err
}

// GetAllActivePaymentSettings 获取所有激活的支付设置
func GetAllActivePaymentSettings() ([]*PaymentSetting, error) {
	o := orm.NewOrm()
	var settings []*PaymentSetting
	_, err := o.QueryTable(new(PaymentSetting)).Filter("status", 1).Filter("is_deleted", 0).OrderBy("group", "sort_order").All(&settings)
	return settings, err
}
